package com.cws.project.cws.homework.controller;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.cws.clazz.domain.Clazz;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.service.IClazzService;
import com.cws.project.cws.clazz.service.IClazzUserService;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;
import com.cws.project.cws.score.domain.HomeworkScore;
import com.cws.project.cws.teacher.domain.Teacher;
import com.cws.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cws.project.cws.homework.domain.Homework;
import com.cws.project.cws.homework.service.IHomeworkService;

/**
 * 作业管理Controller
 *
 * @author xyl
 * @date 2022-01-10
 */
@Controller
@RequestMapping("/cws/homework")
public class HomeworkController extends BaseController {
    private String prefix = "cws/homework";
    @Autowired
    private IClazzService clazzService;
    @Autowired
    private IHomeworkService homeworkService;
    @Autowired
    private IClazzUserService clazzUserService;
    @Autowired
    private ICourseService courseService;

    @RequiresPermissions("cws:homework:view")
    @GetMapping()
    public String homework() {
        return prefix + "/homework";
    }

    @RequiresPermissions("cws:homework:student:view")
    @GetMapping("/student")
    public String studentHomework() {
        return prefix + "/student/homework";
    }


    @GetMapping("/student/submit/{homeworkId}")
    public String studentSubmit(@PathVariable("homeworkId") Long homeworkId, ModelMap mmap) throws Exception {
        Homework homework = homeworkService.selectHomeworkById(homeworkId);
        mmap.put("homework", homework);
        return prefix + "/student/submit";
    }


    @RequiresPermissions("cws:homework:detail")
    @GetMapping("/detail/{homeworkId}")
    public String detail(@PathVariable("homeworkId") Long homeworkId, ModelMap mmap) {
        Homework homework = homeworkService.selectHomeworkById(homeworkId);
        if(getSysUser().isStudent()){
            homework.setStudentId(getUserId());
        }
        mmap.put("homework", homework);
        return prefix + "/detail";
    }

    /**
     * 学生作业列表
     */
    @RequiresPermissions("cws:homework:student:list")
    @PostMapping("/student/list")
    @ResponseBody
    public TableDataInfo studentList(Homework homework) {
        User sysUser = getSysUser();
        homework.setStudentId(sysUser.getUserId());
        startPage();
        List<Homework> list = homeworkService.selectHomeworkList(homework);
        return getDataTable(list);
    }


    /**
     * 查询作业管理列表
     */
    @RequiresPermissions("cws:homework:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Homework homework) {
        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            homework.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher() ){
            homework.setCreateId(sysUser.getUserId());
        }
        startPage();
        List<Homework> list = homeworkService.selectHomeworkList(homework);
        return getDataTable(list);
    }

    /**
     * 导出作业管理列表
     */
    @RequiresPermissions("cws:homework:export")
    @Log(title = "作业管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Homework homework) {
        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            homework.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher() ){
            homework.setCreateId(sysUser.getUserId());
        }
        List<Homework> list = homeworkService.selectHomeworkList(homework);
        ExcelUtil<Homework> util = new ExcelUtil<Homework>(Homework.class);
        return util.exportExcel(list, "homework");
    }

    /**
     * 新增作业管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        User sysUser = getSysUser();
//        List<Clazz> clazzList;
        List<Course> courseList;
        if(sysUser.isAdmin()){
//            clazzList = clazzService.selectClazzList(null);
            courseList = courseService.selectCourseList(null);
        }else{
            Course course = new Course();
            course.setTeacherId(sysUser.getUserId());
//            clazzList = clazzService.selectClazzListByUserId(sysUser.getUserId());
            courseList = courseService.selectCourseList(course);
        }
//        mmap.put("clazzs",clazzList);
        mmap.put("courses",courseList);
        return prefix + "/add";
    }

    /**
     * 新增保存作业管理
     */
    @RequiresPermissions("cws:homework:add")
    @Log(title = "作业管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Homework homework) {
        homework.setStartTime(new Date());
        homework.setCreateId(getSysUser().getUserId());
        return toAjax(homeworkService.insertHomework(homework));
    }

    /**
     * 修改作业管理
     */
    @GetMapping("/edit/{homeworkId}")
    public String edit(@PathVariable("homeworkId") Long homeworkId, ModelMap mmap) {
        //当前辅导员id，
        User sysUser = getSysUser();
//        List<Clazz> clazzList;
        List<Course> courseList;
        if(sysUser.isAdmin()){
//            clazzList = clazzService.selectClazzList(null);
            courseList = courseService.selectCourseList(null);
        }else{
            Course course = new Course();
            course.setTeacherId(sysUser.getUserId());
//            clazzList = clazzService.selectClazzListByUserId(sysUser.getUserId());
            courseList = courseService.selectCourseList(course);
        }
//        mmap.put("clazzs",clazzList);
        mmap.put("courses",courseList);
        Homework homework = homeworkService.selectHomeworkById(homeworkId);
        mmap.put("homework", homework);
        return prefix + "/edit";
    }

    /**
     * 修改保存作业管理
     */
    @RequiresPermissions("cws:homework:edit")
    @Log(title = "作业管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Homework homework) {
        return toAjax(homeworkService.updateHomework(homework));
    }

    /**
     * 删除作业管理
     */
    @RequiresPermissions("cws:homework:remove")
    @Log(title = "作业管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(homeworkService.deleteHomeworkByIds(ids));
    }
}
